package com.txunda.zhuohired.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

import com.txunda.zhuohired.R;
import com.txunda.zhuohired.util.wheel.NumericWheelAdapter2;
import com.txunda.zhuohired.util.wheel.OnWheelScrollListener;
import com.txunda.zhuohired.util.wheel.WheelView;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2017/11/13 09:49
 * 功能描述：选择时间
 * 联系方式：jiahuanchen@126.com
 */

public class SelectJobTimeDialog extends Dialog implements View.OnClickListener {
    private SelectTimeListener selectTimeListener;
    private Context context;
    private String time = "";
    private String time2 = "";
    private WheelView wv_dialog_time_hour;//时
    private WheelView wv_dialog_time_minute;//分
    private WheelView wv_dialog_time_right_hour;//时
    private WheelView wv_dialog_time_right_minute;//分
    String[] xiaoshi_start =
            {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16",
                    "17", "18",
                    "19", "20", "21", "22", "23"};

    String[] fenzhong_start =
            {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16",
                    "17", "18",
                    "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34",
                    "35", "36",
                    "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52",
                    "53", "54",
                    "55", "56", "57", "58", "59"};

    private String curHour = "";
    private String curMinute = "";

    private String curRightHour = "";
    private String curRightMinute = "";

    public SelectJobTimeDialog(Context context, SelectTimeListener
            selectTimeListener) {
        super(context, R.style.dialog_style);
        this.context = context;
        this.selectTimeListener = selectTimeListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_time);
        wv_dialog_time_hour = findViewById(R.id.wv_dialog_time_hour);
        wv_dialog_time_minute = findViewById(R.id.wv_dialog_time_minute);
        wv_dialog_time_right_hour = findViewById(R.id.wv_dialog_time_right_hour);
        wv_dialog_time_right_minute = findViewById(R.id.wv_dialog_time_right_minute);
        findViewById(R.id.tv_dialog_time_cancel).setOnClickListener(this);
        findViewById(R.id.tv_dialog_time_sure).setOnClickListener(this);
        initWheelView();
        initWheelView2();
    }

    @Override
    public void onClick(View view) {
        this.dismiss();
        switch (view.getId()) {
            case R.id.tv_dialog_time_sure:
                selectTimeListener.onSelectTime(time + "-" + time2);
                break;
        }
    }


    public interface SelectTimeListener {
        void onSelectTime(String time);
    }


    private void initWheelView() {
        Calendar c = Calendar.getInstance();
        NumericWheelAdapter2 numericAdapter1 = new NumericWheelAdapter2(context, 0, 23);
        numericAdapter1.setLabel("");
        numericAdapter1.setTextSize(18);
        wv_dialog_time_hour.setViewAdapter(numericAdapter1);
        wv_dialog_time_hour.setCyclic(true);// 可循环滚动
        wv_dialog_time_hour.addScrollingListener(startScrollListener);
        NumericWheelAdapter2 numericAdapter2 = new NumericWheelAdapter2(context, 0, 59);
        numericAdapter2.setLabel("");
        numericAdapter2.setTextSize(18);
        wv_dialog_time_minute.setViewAdapter(numericAdapter2);
        wv_dialog_time_minute.setCyclic(true);// 可循环滚动
        wv_dialog_time_minute.addScrollingListener(startScrollListener);


        String currenthh = new SimpleDateFormat("HH").format(c.getTime());
        List<String> asList = Arrays.asList(xiaoshi_start);
        int hour_index = asList.indexOf(currenthh);

        String currentmm = new SimpleDateFormat("mm").format(c.getTime());
        List<String> asList2 = Arrays.asList(fenzhong_start);

        int min_index = asList2.indexOf(currentmm);
        if (hour_index < 10) {
            curHour = "0" + hour_index;
        } else {
            curHour = "" + hour_index;
        }
        if (min_index < 10) {
            curMinute = "0" + min_index;
        } else {
            curMinute = "" + min_index;
        }
        wv_dialog_time_hour.setCurrentItem(hour_index);
        wv_dialog_time_minute.setCurrentItem(min_index);
        time = curHour + ":" + curMinute;
    }

    OnWheelScrollListener startScrollListener = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {
        }

        @Override
        public void onScrollingFinished(WheelView wheel) {
            int hour = wv_dialog_time_hour.getCurrentItem();
            int minute = wv_dialog_time_minute.getCurrentItem();
            if (hour < 10) {
                curHour = "0" + hour;
            } else {
                curHour = "" + hour;
            }
            if (minute < 10) {
                curMinute = "0" + minute;
            } else {
                curMinute = "" + minute;
            }
            time = curHour + ":" + curMinute;
        }
    };

    private void initWheelView2() {
        Calendar c = Calendar.getInstance();
        NumericWheelAdapter2 numericAdapter1 = new NumericWheelAdapter2(context, 0, 23);
        numericAdapter1.setLabel("");
        numericAdapter1.setTextSize(18);
        wv_dialog_time_right_hour.setViewAdapter(numericAdapter1);
        wv_dialog_time_right_hour.setCyclic(true);// 可循环滚动
        wv_dialog_time_right_hour.addScrollingListener(startScrollListener2);
        NumericWheelAdapter2 numericAdapter2 = new NumericWheelAdapter2(context, 0, 59);
        numericAdapter2.setLabel("");
        numericAdapter2.setTextSize(18);
        wv_dialog_time_right_minute.setViewAdapter(numericAdapter2);
        wv_dialog_time_right_minute.setCyclic(true);// 可循环滚动
        wv_dialog_time_right_minute.addScrollingListener(startScrollListener2);


        String currenthh = new SimpleDateFormat("HH").format(c.getTime());
        List<String> asList = Arrays.asList(xiaoshi_start);
        int hour_index = asList.indexOf(currenthh);

        String currentmm = new SimpleDateFormat("mm").format(c.getTime());
        List<String> asList2 = Arrays.asList(fenzhong_start);

        int min_index = asList2.indexOf(currentmm);
        if (hour_index < 10) {
            curRightHour = "0" + hour_index;
        } else {
            curRightHour = "" + hour_index;
        }
        if (min_index < 10) {
            curRightMinute = "0" + min_index;
        } else {
            curRightMinute = "" + min_index;
        }
        wv_dialog_time_right_hour.setCurrentItem(hour_index);
        wv_dialog_time_right_minute.setCurrentItem(min_index);
        time2 = curRightHour + ":" + curRightMinute;
    }

    OnWheelScrollListener startScrollListener2 = new OnWheelScrollListener() {
        @Override
        public void onScrollingStarted(WheelView wheel) {
        }

        @Override
        public void onScrollingFinished(WheelView wheel) {
            int hour = wv_dialog_time_right_hour.getCurrentItem();
            int minute = wv_dialog_time_right_minute.getCurrentItem();
            if (hour < 10) {
                curRightHour = "0" + hour;
            } else {
                curRightHour = "" + hour;
            }
            if (minute < 10) {
                curRightMinute = "0" + minute;
            } else {
                curRightMinute = "" + minute;
            }
            time2 = curRightHour + ":" + curRightMinute;
        }
    };

}
